package com.arcvideo.acsconnect.ui.presenter;

import android.content.Context;

import com.arcvideo.acsconnect.app.AccessAction;
import com.arcvideo.acsconnect.app.AcsConnectApplication;
import com.arcvideo.acsconnect.rest.model.Account;
import com.arcvideo.acsconnect.rest.model.ElderFamily;
import com.arcvideo.acsconnect.rest.model.FamilyInfo;
import com.arcvideo.acsconnect.rest.model.ResultBean;
import com.arcvideo.acsconnect.rest.model.UserBean;
import com.arcvideo.acsconnect.rest.service.AppService;
import com.arcvideo.acsconnect.ui.view.LiveView;
import com.arcvideo.arcrtcsdk.ArcRtcSDK;
import com.arcvideo.rtcmessage.model.RtcClientModel;
import com.framework.core.bean.RestError;
import com.framework.core.mvp.BasePresenter;
import com.framework.core.mvp.IObserver;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/11/16
 * @description
 */

public class LivePresenter extends BasePresenter<LiveView> {

    AppService mAppService;

    public LivePresenter() {
        super();
        mAppService = mRetrofit.create(AppService.class);
    }

    public void getAccountsByPage(final Context context) {
        UserBean userBean = AcsConnectApplication.sUserList.get(AcsConnectApplication.sCurrentUserIndex);
        Observable<ResultBean<List<Account>>> getNurses = mAppService.getAccountsByPage(
                AccessAction.GET_ACCOUNTS_BY_PAGE,
                System.currentTimeMillis() + "",
                "",
                "3,5"
        );
        Observable<ResultBean<List<Account>>> getFamily = mAppService.getAccountsByPage(
                AccessAction.GET_ACCOUNTS_BY_PAGE,
                System.currentTimeMillis() + "",
                userBean.getId(),
                ""
        );
        Observable.zip(getNurses, getFamily, new Func2<ResultBean<List<Account>>, ResultBean<List<Account>>, List<UserBean>>() {

            @Override
            public List<UserBean> call(ResultBean<List<Account>> listResultBean, ResultBean<List<Account>> listResultBean2) {
                Account accountElder = listResultBean2.getResult().get(0);
                List<ElderFamily> familyList = accountElder.getElderFamilyList();
                List<UserBean> userList = new ArrayList<>();
                Map<String, RtcClientModel> map = ArcRtcSDK.create(context).getClientMap();
                //添加护士到邀请列表
                for (Account account : listResultBean.getResult()) {
                    UserBean userBean = buildSimpleUserBean(account.getAccount(),
                            account.getId(), account.getHeadPicUrl(), account.getName());
                    userBean.setOnline(map.containsKey(userBean.getId()));
                    userList.add(userBean);
                }
                //添加老人家属到邀请列表
                for (ElderFamily elderFamily : familyList) {
                    FamilyInfo familyInfo = elderFamily.getFamilyInfo();
                    UserBean userBean = buildSimpleUserBean(familyInfo.getAccount(),
                            familyInfo.getId(), familyInfo.getHeadPicUrl(), familyInfo.getName());
                    userBean.setOnline(map.containsKey(userBean.getId()));
                    userList.add(userBean);
                }
                return userList;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new IObserver<List<UserBean>>(this, true) {
                    @Override
                    public boolean onFailed(RestError restError) {
                        return false;
                    }

                    @Override
                    public void onNext(List<UserBean> userList) {
                        getMvpView().getUserListSuccess(userList);
                    }
                });
    }

    private UserBean buildSimpleUserBean(String account, String id, String avatar, String name) {
        UserBean userBean = new UserBean();
        userBean.setAccount(account);
        userBean.setId(id);
        userBean.setHeadPicUrl(avatar);
        userBean.setName(name);
        return userBean;
    }
}
